package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.util.XzqhUtils;
import cn.com.nes.mybatis.agent.system.entity.TUser;
import cn.com.nes.mybatis.callcenter.entity.WorkOrderInfo;
import cn.com.nes.mybatis.callcenter.mapper.CallWorkOrderInfoMapper;
import cn.com.nes.site.entity.em.WorkOrderStautsEnum;
import cn.com.nes.site.service.system.user.SystemUserService;
import cn.com.nes.site.service.workOrder.WorkOrderStatisticService;
import cn.com.nes.site.service.workOrder.bo.GetWorkOrderStatisticalBO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderStatisticalDTO;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 工单统计Service
 *
 * @Author: ShenWei
 * @Date: 2024/09/04/11:06
 */
@Slf4j
@Service
public class WorkOrderStatisticServiceImpl extends ServiceImpl<CallWorkOrderInfoMapper, WorkOrderInfo> implements WorkOrderStatisticService {

    @Resource
    private SystemUserService systemUserService;

    /**
     * 查询统计结果
     */
    @Override
    public WorkOrderStatisticalDTO getWorkOrderStatisticalResult(GetWorkOrderStatisticalBO bo) {
        if (StrUtil.isNotBlank(bo.getAreaCode())) {
            bo.setAreaCode(XzqhUtils.getRegionCodePrefix(bo.getAreaCode()));
        }
        if (ObjectUtil.isNotNull(bo.getMaintainId())) {
            TUser userInfoByUserId = systemUserService.getUserInfoByUserId(String.valueOf(bo.getMaintainId()));
            bo.setCallCenterMaintainId(userInfoByUserId.getCallCenterUserId());
        }
        if (ObjectUtil.isNotNull(bo.getStartTime())) {
            bo.setStartTimeStamp(bo.getStartTime().getTime());
        }
        if (ObjectUtil.isNotNull(bo.getEndTime())) {
            bo.setEndTimeStamp(bo.getEndTime().getTime());
        }

        // 查询创建工单数
        bo.setWorkOrderStatus(Integer.valueOf(WorkOrderStautsEnum._2.getKey()));
        Integer createWorkOrderCount = this.baseMapper.statisticalWorkOrderCount(bo);
        // 查询完成工单数
        bo.setWorkOrderStatus(Integer.valueOf(WorkOrderStautsEnum._7.getKey()));
        Integer completeWorkOrderCount = this.baseMapper.statisticalWorkOrderCount(bo);
        // 查询据单数
        Integer rejectionWorkOrderCount = this.baseMapper.getRejectionWorkOrderCount(bo);
        // 查询撤销工单数
        bo.setWorkOrderStatus(Integer.valueOf(WorkOrderStautsEnum._4.getKey()));
        Integer cancelWorkOrderCount = this.baseMapper.statisticalWorkOrderCount(bo);

        // 准备返回值
        WorkOrderStatisticalDTO workOrderStatisticalDTO = new WorkOrderStatisticalDTO();
        workOrderStatisticalDTO.setCreateWorkOrderCount(createWorkOrderCount);
        workOrderStatisticalDTO.setCompleteWorkOrderCount(completeWorkOrderCount);
        workOrderStatisticalDTO.setRejectionWorkOrderCount(rejectionWorkOrderCount);
        workOrderStatisticalDTO.setCancelWorkOrderCount(cancelWorkOrderCount);
        return workOrderStatisticalDTO;
    }
}
